package testProxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import testProxy.DBQuery;
import testProxy.IDBQuery;

public class JDKDynamicProxyHandler implements InvocationHandler {
	private IDBQuery proxied = null;

	/**
	 * 创建动态代理处理对象
	 * 
	 * @param proxied
	 */
	public JDKDynamicProxyHandler(IDBQuery proxied) {
		this.proxied = proxied;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// 调用被代理的对象的方法
		return method.invoke(proxied, args);
	}
	
	
	public static IDBQuery createDynamicProxy(){
        IDBQuery proxy=(IDBQuery)Proxy.newProxyInstance(IDBQuery.class.getClassLoader(), 
                new Class[]{IDBQuery.class}, 
                new JDKDynamicProxyHandler(new DBQuery()));
        return proxy;
    }
}
